package com.mashibing.mall.product.service.impl;

import com.mashibing.common.utils.PageUtils;
import com.mashibing.common.utils.Query;
import com.mashibing.mall.product.param.AttrGroupRelationParam;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.mashibing.mall.product.dao.AttrAttrgroupRelationDao;
import com.mashibing.mall.product.entity.AttrAttrgroupRelationEntity;
import com.mashibing.mall.product.service.AttrAttrgroupRelationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 属性&属性分组关联 service 实现类
 */
@Service("attrAttrgroupRelationService")
@Transactional
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     *  属性&属性分组关联 - 关联属性
     */
    @Override
    public void relateAttributes(List<AttrGroupRelationParam> params) {
        List<AttrAttrgroupRelationEntity> list = params.stream().map(param -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            entity.setAttrGroupId(param.getAttrGroupId());
            entity.setAttrId(param.getAttrId());
            return entity;
        }).collect(Collectors.toList());
        this.saveBatch(list);
    }

}
